//
//  WithdrawTaskCell.swift
//  MT
//
//  Created by 陈悦 on 2020/5/7.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

@objc
protocol WithdrawTaskCellDelegate: NSObjectProtocol {
    /// 分享
    @objc optional func shareClick()
}

class WithdrawTaskCell: UITableViewCell {
    
    /// 代理
    weak var delegate: WithdrawTaskCellDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
//    lazy var dataLabel: UILabel = {
//        let l = UILabel()
//        l.font = PingFangSCRegularFont(14)
//        l.textColor = .white
//        return l
//    }()
    
    var model: MTWithdrawReportSubModel? {
        didSet {
            guard let model = model else { return }
            detailLabel.text = model.remark
            finishButton.setTitle(model.status == 1 ? "已完成" : "去完成", for: .normal)
        }
    }
    
    lazy var detailLabel: UILabel = {
        let l = UILabel()
        l.textColor = UIColor(hex:0x1d1a1a)
        l.font = PingFangSCRegularFont(12)
        l.numberOfLines = 0
        return l
    }()
    
    lazy var finishButton: UIButton = {
        let btn = UIButton()
        btn.backgroundColor = UIColor(hex:0x426bf2)
        btn.layer.cornerRadius = 4
        btn.setTitle("去完成", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = PingFangSCSemiboldFont(12)
        btn.addTarget(self, action: #selector(shareButtonClick), for: .touchUpInside)
        return btn
    }()
    
    lazy var finishLab: UILabel = {
        let lab = UILabel()
        lab.textColor = UIColor(hex:0x1d1a1a)
        lab.font = PingFangSCSemiboldFont(14)
        lab.textAlignment = .right
        lab.text = "已完成"
        return lab
    }()
    
    lazy var leftTimeLab: UILabel = {
        let lab = UILabel()
        lab.textColor = UIColor(hex:0x9b9b9b)
        lab.font = PingFangSCMediumFont(12)
        lab.textAlignment = .right
        lab.text = "剩余23小时"
        return lab
    }()
    
    lazy var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex:0xd8d8d8)
        return v
    }()
    
    
    class func reuseIdentity() -> String {
        return "WithdrawTaskCellID"
    }
    
    func bind(model: MTLuckyReportSubModel) {
            
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        makeConstraint()
        contentView.backgroundColor = UIColor.theme.background
        backgroundColor = UIColor.theme.background
    }
    
    private func setupUI() {
        contentView.addSubview(finishButton)
        contentView.addSubview(finishLab)
        contentView.addSubview(leftTimeLab)
        contentView.addSubview(detailLabel)
        contentView.addSubview(lineView)
    }
    
    private func makeConstraint() {
        
        detailLabel.snp.makeConstraints { (make) in
            make.top.equalTo(contentView).offset(25)
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-100)
            make.bottom.equalTo(contentView).offset(-25)
        }
        
        finishButton.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-10)
            make.width.equalTo(60)
            make.height.equalTo(28)
        }
        
//        finishLab.snp.makeConstraints { (make) in
//            make.centerX.equalTo(contentView).offset(-10)
//            make.right.equalToSuperview().offset(-20)
//        }
//
//        leftTimeLab.snp.makeConstraints { (make) in
//            make.top.equalTo(finishLab.snp.bottom).offset(3)
//            make.right.equalToSuperview().offset(-20)
//        }

        lineView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

}

@objc
extension WithdrawTaskCell {
    private func shareButtonClick() {
        if let delegate = delegate {
            delegate.shareClick?()
        }
    }
}

